<template>
  <div id="add">
    <div class="add">
      <el-form :rules="rules" :model="form">
        <el-form-item prop="sayName">
          <el-input v-model="form.sayName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item prop="sayEname">
          <el-input
            v-model="form.sayEname"
            placeholder="Please enter the English name"
          />
        </el-form-item>
        <!-- 上传 -->
        <el-form-item>
          <el-upload
            v-model:file-list="fileList"
            action="#"
            list-type="picture-card"
            :auto-upload="false"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <el-icon><Plus /></el-icon>
          </el-upload>
          <!-- 上传浏览 -->
          <el-dialog v-model="dialogVisible">
            <img class="pImg" :src="dialogImageUrl" alt="预览图片" />
          </el-dialog>
        </el-form-item>
        <!-- 介绍 -->
        <el-form-item prop="sayContent">
          <el-input
            v-model="form.sayContent"
            type="textarea"
            placeholder="请输入名言警句"
            :rows="3"
            resize="none"
          />
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button type="primary" @click="addAph">新增名言警句</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { Plus } from "@element-plus/icons-vue";
import {
  ElMessage,
  FormRules,
  UploadProps,
  UploadUserFile,
} from "element-plus";
//后端请求了
import { aphAdd } from "../../../../api/index/aph";
//调用父组件定义
const emit = defineEmits(["addSuccess"]);
//添加成功
const addAph = () => {
  aphAdd(form.value).then((res) => {
    ElMessage({
      type: "success",
      message: res + "",
    });
    emit("addSuccess"); //调用父组件,创建成功
  });
};
//链接表单和规则
const form = ref({
  sayName: "",
  sayEname: "",
  sayImg: "",
  sayContent: "",
});
const rules = reactive<FormRules>({
  sayName: [
    { required: true, message: "请输入名字", trigger: "blur" },
    { max: 50, message: "不要超过50", trigger: "blur" },
  ],
  sayEname: [
    { required: true, message: "请输入英文名", trigger: "blur" },
    { max: 50, message: "不要超过50", trigger: "blur" },
  ],
  sayContent: [
    { required: true, message: "请输入名言警句", trigger: "blur" },
    {
      min: 10,
      max: 150,
      message: "不得低于10字,也不要超过150字",
      trigger: "blur",
    },
  ],
});
//默认加载图片
const fileList = ref<UploadUserFile[]>([]);
//显示图片
const dialogImageUrl = ref("");
//显示预览上传的图片
const dialogVisible = ref(false);
//删除图片
const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};
//浏览图片
const handlePictureCardPreview: UploadProps["onPreview"] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!;
  dialogVisible.value = true;
};
</script>

<style lang="less" scoped>
#add {
  width: 100%;
  height: 100%;
  .add {
    :deep(.el-input__wrapper) {
      --el-input-focus-border-color: aqua;
      background-color: rgb(27, 44, 92);
      .el-input__inner {
        color: white;
        &::-webkit-input-placeholder {
          /* WebKit browsers*/
          color: rgba(aqua, 0.5);
        }
      }
    }
    :deep(.el-textarea) {
      // 一下为正常
      background-color: transparent;
      .el-textarea__inner {
        overflow: hidden;
        overflow-y: scroll;
        &::-webkit-scrollbar {
          width: 2px;
          height: 100%;
        }
        &::-webkit-scrollbar-thumb {
          /*滚动条里面小方块*/
          border-radius: 2px;
          background: orange;
        }
        &::-webkit-scrollbar-track {
          /*滚动条里面轨道*/
          border-radius: 2px;
          background: rgba(blue, 0.2);
        }
        background-color: rgb(27, 44, 92);
        --el-input-focus-border-color: aqua;
        color: white;
        &::-webkit-input-placeholder {
          /* WebKit browsers*/
          color: rgba(aqua, 0.5);
        }
      }
    }
    :deep(.el-dialog) {
      width: 35%;
      --el-dialog-bg-color: rgba(255, 119, 0, 0.1);
      border-image-source: radial-gradient(
        65% 65%,
        transparent 0px,
        transparent 100%,
        orange 100%
      );
      .el-dialog__title {
        color: orange;
      }
      .el-dialog__close {
        color: orange;
      }
    }
    .pImg {
      width: 100%;
    }
    .btn {
      margin-top: 5%;
      margin-left: 75%;
      :deep(.el-button--primary) {
        margin-bottom: 5%;
        --el-button-bg-color: none;
        --el-button-border-color: rgba(aqua, 0.8);
        --el-button-hover-text-color: black;
        --el-button-hover-bg-color: rgba(aqua, 0.8);
        --el-button-hover-border-color: rgba(aqua, 0.8);
      }
    }
  }
}
</style>
